<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var forumZoneTree;
	$(function() {
		$('#admin_forumZone_panel').panel({
			title : '论坛分区',
			fit : true,
			border : false,
			tools : [ {
				iconCls : 'icon-add',
				handler : function() {
					addForumZone();
				}
			}, {}, {
				iconCls : 'icon-remove',
				handler : function() {
					removeForumZone();
				}
			}, {} ]
		});

		function addForumZone() {
			$('#admin_forumZoneAdd_addForumZoneForm input').val('');
			var selectNode = forumZoneTree.tree('getSelected');
			if (selectNode != null) {
				var dlg = $('<div style="padding:20px 20px;" />').dialog({
					width : 400,
					height : 200,
					href : '${pageContext.request.contextPath}/pages/admin/forumZoneAdd.jsp',
					modal : true,
					title : '论坛分区新增',
					buttons : [ {
						text : '保存',
						handler : function() {
							$('#admin_forumZoneAdd_addForumZoneForm').form('submit', {
								url : '${pageContext.request.contextPath}/treeAction!addTreeNode.action',
								success : function(r) {
									var obj = jQuery.parseJSON(r);
									if (obj.success) {
										freshForumZone();
										dlg.dialog('close');
									}
									$.messager.show({
										title : '提示',
										msg : obj.msg,
									});
								}
							});
						}
					}, {
						text : '取消',
						handler : function() {
							$('#admin_forumZoneAdd_addForumZoneForm input').val('');
							dlg.dialog('close');
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						$('#admin_forumZoneAdd_addForumZoneForm').form('load', {
							ptext : selectNode.text,
							pid : selectNode.id
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择文档类型！');
			}

		}

		function removeForumZone() {
			var selectNode = forumZoneTree.tree('getSelected');
			if (selectNode != null) {
				var selectNodeId = selectNode.id;
				var rootNode = forumZoneTree.tree('getRoot');
				var rootNodeId = rootNode.id;
				if (selectNodeId != rootNodeId) {
					var flag = forumZoneTree.tree('isLeaf', selectNode.target);
					if (flag) {
						$.ajax({
							url : '${pageContext.request.contextPath}/treeAction!removeTreeNode.action',
							data : {
								type : '03',
								id : selectNode.id
							},
							dataType : 'json',
							success : function(r) {
								freshForumZone();
								if (r.success) {
									$.messager.show({
										title : '提示',
										msg : r.msg
									});
								} else {
									$.messager.alert('提示', r.msg);
								}
							}
						});
					} else {
						$.messager.alert('提示', '只允许从叶子节点开始删除！');
					}
				} else {
					$.messager.alert('提示', '根节点不允许删除！');
				}
			} else {
				$.messager.alert('提示', '请选择要删除的节点！');
			}
		}

		forumZoneTree = $('#admin_forumZone_tree').tree({
			url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=03',
			parentField : 'pid',
			lines : true
		});

		function freshForumZone() {
			forumZoneTree.tree({
				url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=03',
				parentField : 'pid',
			});
		}

	});
</script>
<div id="admin_forumZone_panel" style="padding:5px 5px 0 5px">
	<ul id="admin_forumZone_tree"></ul>
</div>